function isOperator(value){
    var operatorString = "+-*/()";
    return operatorString.indexOf(value) > -1
}
 
function getPrioraty(value){
    switch(value){
        case '+':
        case '-':
            return 1;
        case '*':
        case '/':
            return 2;
        default:
            return 0;
    }
}
 
function prioraty(o1, o2){
    return getPrioraty(o1) <= getPrioraty(o2);
}
 
export function dal2Rpn(exp){
    let errorInfo = '';
    let inputStack = [];
    let outputStack = [];
    let outputQueue = [];
 
    for(let i = 0, len = exp.length; i < len; i++){
        let cur = exp[i];
        if(cur !== ' ' ){
            inputStack.push(cur);
        }
    }
    // console.log('step one');
    while(inputStack.length > 0){
    	let cur = inputStack.shift();
    	let strs = cur;
        if(!isOperator(cur)){
        	while(inputStack.length > 0 && !isOperator(cur)){
        		cur = inputStack.shift();
        		if(!isOperator(cur)){
        			strs += cur;
        		} 
        	}
        	outputQueue.push(parseFloat(strs));
        }

        	if(isOperator(cur)){
	        		if(cur === '('){
		                outputStack.push(cur);
		            }else if(cur === ')'){
		                let po = outputStack.pop();
		                while(po !== '(' && outputStack.length > 0){
		                    outputQueue.push(po);
		                    po = outputStack.pop();
		                }
		                if(po !== '('){
		                    // throw new Error(alert('sfsf'));
                            // alert('括号不匹配！')
                            errorInfo = '括号不匹配！';
		                }
		            }else{
		                while(prioraty(cur, outputStack[outputStack.length - 1]) && outputStack.length > 0){
		                    outputQueue.push(outputStack.pop());
		                }
		                outputStack.push(cur);
		            }
        	}
    }
    // console.log('step two');
    if(outputStack.length > 0){
        if(outputStack[outputStack.length - 1] === ')' || outputStack[outputStack.length - 1] === '('){
            // throw "error: unmatched ()";
            // alert('括号不匹配！')
            errorInfo = '括号不匹配！';

        }
        while(outputStack.length > 0){
            outputQueue.push(outputStack.pop());
        }
    }
    // console.log('step three');
    return {outputQueue,errorInfo};
 
}

export function evalRpn(rpnQueue){
    let errorInfo= '';
    let outputStack = [];
    while(rpnQueue.length > 0){
        let cur = rpnQueue.shift();
 
        if(!isOperator(cur)){
            outputStack.push(cur);
        }else{
            if(outputStack.length < 2){
                // throw "unvalid stack length";
                // alert('请输入正确的表达式')
                errorInfo = '请输入正确的表达式';

                outputStack = [];
            }else{
            let sec = outputStack.pop();
            let fir = outputStack.pop();
            outputStack.push(getResult(fir, sec, cur));
            }
        }
    }
    if(outputStack.length > 1){
        // throw "unvalid expression";
        // alert('请输入正确的表达式')
        errorInfo = '请输入正确的表达式';
        outputStack = [];

    }else{
        // console.log(outputStack)
        return {outputStack,errorInfo};
    }
}

function getResult(x,y,z){
	switch(z){
        case '+':
        	return x + y ;
        case '-':
            return x - y ;
        case '*':
        	return x * y ;
        case '/':
            return x / y ;
        default:
            return 0;
    }
}